﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Barbar.MapReport.Common.Drawing.ValueIndicators;
using Barbar.MapReport.HtmlRender.Shapes;

namespace Barbar.MapReport.HtmlRender.Operations.ValueIndicators {
  internal class ValueIndicatorAgregatorGroupRenderer : IValueIndicatorAgregatorRenderer {
    private GroupProperties m_Properties;

    public ValueIndicatorAgregatorGroupRenderer(GroupProperties properties) {
      m_Properties = properties;
    }

    #region IValueIndicatorAgregatorRenderer Members

    public ShapeWithTooltip Render(ValueIndicatorAgregator agregator) {
      bool isOfSameType = true;
      foreach (FlatPoint indicator in agregator.Items) {
        if (indicator.Value1 != agregator.Items[0].Value1) {
          isOfSameType = false;
          break;
        }
      }
     
      Color overrideColor = m_Properties.GroupColor.Color;
      if (m_Properties.Groups.Count != 0 && (agregator.Items.Count == 1 || isOfSameType)) {
        foreach (Group group in m_Properties.Groups) {
          if (group.Index == (int)agregator.Items[0].Value1)
            overrideColor = group.Color.Color;
        }
      }

      int realRadius = agregator.RealRadius;
      IShape shape;
      if (agregator.Items.Count == 1)
        shape = new CircleShape(realRadius, overrideColor);
      else
        shape = new CircleWithNumberShape(realRadius, overrideColor, agregator.Items.Count);

      SortedList<int, int> groups = new SortedList<int, int>();
      foreach (FlatPoint valueIndicator in agregator.Items) {
        if (!groups.ContainsKey((int)valueIndicator.Value1))
          groups.Add((int)valueIndicator.Value1, 1);
        else
          groups[(int)valueIndicator.Value1] = groups[(int)valueIndicator.Value1] + 1;
      }

      string toolTip = null;
      switch (m_Properties.ToolTipType) {
        case GroupToolTipType.Aggregate: {
            StringBuilder builder = new StringBuilder();
            builder.Append("<div class=\"popup\">");
            builder.Append(m_Properties.ToolTipTemplate);
            builder.Append("<table colspan=\"0\" border=\"0\">");

            foreach (Group group in m_Properties.Groups) {
              if (groups.ContainsKey(group.Index))
                builder.AppendFormat(@"<tr><td><div style=""width:10px; height:10px; border 1px solid black; background-color:{0}"">&nbsp;</div></td><td class=""fieldBold"">{1}</td><td class=""field"">{2}</td></tr>", group.Color.HtmlColor, group.Name, groups[group.Index]);
            }

            builder.AppendFormat(@"<tr><td>&nbsp;</td><td class=""fieldBold"">Total</td><td class=""field"">{0}</td></tr>", agregator.Items.Count);
            builder.Append("</table>");
            builder.Append("</div>");
            toolTip = builder.ToString();
            break;
          }
        case GroupToolTipType.SimpleList:
          toolTip = ValueIndicatorAgregatorListRenderer.CreateToolTip(m_Properties.ToolTipTemplate, agregator.Items, m_Properties.MaxToolTipAggregated);
          break;
        default:
          throw new NotImplementedException();
      }

      return new ShapeWithTooltip(shape, toolTip);
    }

    #endregion
  }
}
